@echo off
chcp 65001 >nul
echo ==========================================
echo        Desktop Alarm - Complete Build
echo ==========================================

echo.
echo [1/5] Environment Check...
python --version >nul 2>&1
if errorlevel 1 (
    echo ERROR: Python not found
    pause
    exit /b 1
)

echo.
echo [2/5] Cleanup...
if exist build rmdir /s /q build
if exist dist rmdir /s /q dist
if exist Output rmdir /s /q Output
if exist "AlarmClock.spec" del "AlarmClock.spec"
if exist "桌面闹钟.spec" del "桌面闹钟.spec"
echo OK: Cleanup completed

echo.
echo [3/5] PyInstaller Build...
pyinstaller --noconfirm --windowed --name "AlarmClock" --add-data "resources;resources" --add-data "config.ini;." --icon="resources/icons/icon2.ico" main.py

echo.
echo [4/5] Post-processing...
if exist "dist\AlarmClock\AlarmClock.exe" (
    echo OK: PyInstaller successful
    
    echo Renaming to Chinese names...
    if exist "dist\桌面闹钟" rmdir /s /q "dist\桌面闹钟"
    ren "dist\AlarmClock" "桌面闹钟"
    
    if exist "dist\桌面闹钟\AlarmClock.exe" (
        ren "dist\桌面闹钟\AlarmClock.exe" "桌面闹钟.exe"
        echo OK: Files renamed successfully
        
        echo.
        echo [5/5] Creating Installer with fixed setup...
        where iscc >nul 2>&1
        if not errorlevel 1 (
            echo Using setup_fixed.iss for correct file structure...
            iscc setup_fixed.iss
            if exist "Output\桌面闹钟安装程序.exe" (
                echo.
                echo ========================================
                echo SUCCESS: Build completed successfully!
                echo ========================================
                echo.
                echo Files created:
                echo - Executable: dist\桌面闹钟\桌面闹钟.exe
                echo - Internal files: dist\桌面闹钟\_internal\
                echo - Installer: Output\桌面闹钟安装程序.exe
                echo.
                echo Your new filter functionality is included!
                echo.
            ) else (
                echo WARNING: Installer creation failed
                echo But executable is ready at: dist\桌面闹钟\桌面闹钟.exe
            )
        ) else (
            echo WARNING: Inno Setup not found
            echo Executable ready at: dist\桌面闹钟\桌面闹钟.exe
            echo Please install Inno Setup to create installer
        )
    ) else (
        echo ERROR: Failed to rename executable
    )
) else (
    echo ERROR: PyInstaller failed
    echo Check the error messages above
)

echo.
echo ==========================================
echo.
echo To test your application:
echo 1. Run: dist\桌面闹钟\桌面闹钟.exe
echo 2. Check the new filter buttons above the alarm list
echo 3. Test switching between "仅启用", "仅禁用", and "全部"
echo.
pause